$(function() {
	
	
	/*
	用户名校验,检查用户名的长度，是否合法，是否已注册，若有错误则focus输入框
	*/
	var saleName=$("#saleNameMana");
	saleName.blur(function(){
		var uCheck=$("#uCheck");
		$.ajax({
			type:"post",
			url:"check",
			data:{
				saleName:$("#saleNameMana").val()
			},
			dataType:"json",
			success:function(data){
				if (data == 3) {
					uCheck.html("此用户名可以注册！").css({color : "blue"});					
				} else{
					if(data == 0){
						uCheck.html("此用户名已被占用，请重新选择用户名！").css({color: "red"});
					}else if( data  == 1){
						uCheck.html("用户名长度在6到30位之间！").css({color:"red"});
	  				}else if( data == 2){
	  					uCheck.html("用户名只能包含由字母、数字或“_”！").css({color: "red"});
	  				}		
					 saleName.focus();
					}
				},
			error:function() {
				alert("异常");
			}
				
		});
			
		});
	
	//昵称校验
	//电话校验
	//校验联系电话
	var phoneNum=$("#saleDetailPhoneMana");
	var len = $("#len");
	phoneNum.blur(function(){
		//alert("遗失光标");
		var phone=$("#saleDetailPhoneMana").val();
		if(phone.length<11)
		{
			len.html("电话号码不足十一位").css({color: "red" });
			phoneNum.focus();
		}else if (phone.length>11) {
			len.html("电话号码大于十一位").css({color: "red" });
			phoneNum.focus();
		}else if (phone.substr(0,1)!=1){
			len.html("电话号码不是以1开头").css({color: "red" });
			phoneNum.focus();
		}else{
			len.html("Ok!").css({color: "blue" });
		}

		});
	//取消信息修改按钮
	var resetInfoButn=$("#resetInfoButn");
	resetInfoButn.click(function(){
		//alert("取消");
		//$("#saleNameMana").val("");
		$("#saleDetailNicknameMana").val("");
		$("#saleDetailGenderMana").val("");
		$("#saleDetailAddsressMana").val("");
		$("#saleDetailPhoneMana").val("");
		$("#cardTypeMana").val("");
		$("#saleDetailrealnameMana").val("")
		$("#saleTypeMana").val("");
	});
	//提交信息修改按钮
	var modifyInfoButn=$("#modifyInfoButn");
	var modifyInfoButn=$("#modifyInfoButn");
	modifyInfoButn.click(function(){
		//alert("modify");
		$.ajax({
			type:"post",
			url:"../saleInfo/modifyInfo",
			data:{
				accountId:$("#saleIdMana").val(),
				saleAccount:$("#saleNameMana").val(),
				realname:$("#saleDetailrealnameMana").val(),
				nickname:$("#saleDetailNicknameMana").val(),
				gender:$("#saleDetailGenderMana").val(),
				saleAddress:$("#saleDetailAddsressMana").val(),
				phone:$("#saleDetailPhoneMana").val()
			},
			dataType:"json",
			success:function(){
				alert("修改成功");
				window.location.href="../saleInfo/getSaleInfo";
			},
			error:function(){
				alert("修改异常");
			}
		})
		});
	//密码修改校验
	var oldpsw=$("#oldPW");
	var psw1= $("#psw1");
	
	oldpsw.blur(function(){
		var oldVal=$("#oldPW").val();
			if(oldVal.length<6)
		{
			psw1.html("密码长度不足六位！").css({ color:"red"});	
		
		}else if(oldVal.length>6){
			
				psw1.html("密码长度大于六位！").css({ color:"red"});	
		}else{
			psw1.html("OK！").css({ color:"blue"});
		}
	});

	var newpsw=$("#newPW");
	var psw2=$("#psw2");
	newpsw.blur(function(){
		var newVal=$("#newPW").val();
		if(newVal.length<6){
			psw2.html("密码长度不足六位！").css({ color:"red"});
			
		}else if (newVal.length>6) {
			psw2.html("密码长度大于六位！").css({ color:"red"});
		}else{
			psw2.html("OK！").css({ color:"blue"});
		}
	});
	var checkpsw=$("#checkPW");
	
	var psw3=$("#psw3");
	checkpsw.blur(function(){

		var newVal=$("#newPW").val();
		var checkVal=$("#checkPW").val();
		
		if (newVal!=checkVal) {
			psw3.html("两次输入的密码不一样！").css({ color:"red"});
		}else
		{
			psw3.html("OK！").css({ color:"blue"});
		}

	})
	
	//取消密码修改按钮
	var resetPWButn=$("#resetPWButn");
	resetPWButn.click(function(){
		//alert("quiao");
		newpsw.val("");
		oldpsw.val("");
		checkpsw.val("");
		psw1.html("");
		psw2.html("");
		psw3.html("");
	})
	//提交密码修改按钮
	var modifyPWButn=$("#modifyPWButn");
	modifyPWButn.click(function(){
		$.ajax({
			type:"post",
			url:"../saleInfo/modifyPW",
			data:{
				oldpsw:MD5($("#oldPW").val()),
				newpsw:MD5($("#newPW").val())
			},
			dataType:"json",
			success:function(data){
				if(data==0){
					alert("用户名出现异常");
				}else if(data==2){
					alert("密码不匹配，修改不成功")
				}else if(data==1){
					alert("密码修改成功");
				}
			},
			error:function(){
				alert("修改异常");
			}
		
		})
	})
	})	

